angular.module('start.guide.guideorder.ctrl', [])

  .controller('guideorderCtrl', function($scope, $rootScope, $$GuideService, $stateParams, $$UserOrderService, $location, $ionicLoading, $timeout,$ionicPickerI18n) {
    console.log("guideorderCtrl");
    $scope.guideId = $stateParams.guideId;

    $ionicPickerI18n.weekdays = ["日", "一", "二", "三", "四", "五", "六"];
    $ionicPickerI18n.months = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
    $ionicPickerI18n.ok = "确定";
    $ionicPickerI18n.cancel = "取消";
    $ionicPickerI18n.okClass = "button-positive";
    $ionicPickerI18n.cancelClass = "button-stable";
    $ionicPickerI18n.arrowButtonClass = "button-positive";

    $scope.guideVO = {
      grade: 5,
      guideId: "571e67554000000",
      headPic: "www.bannitongyou.com/pic/head.jpg",
      lifePic: "www.bannitongyou.com/pic/img/tx.png",
      serviceCity: "",
      servicePrice: null,
      serviceRoute: "",
      type: 1,
      userName: "",

      shopping: 0,
      travel: null,
      business: 0,
      food: 0,
    };

    $scope.nowDate = new Date().getTime();
    $scope.orderGuideData = {
      contactPhone: "13388768745", // "string"
      guideState: 0, // 0
      guideId: $scope.guideId,
      price: null, // 0
      realName: "realName", // "string"
      remarks: "", // "string"
      travelStartDate: new Date($scope.nowDate), // "string"
      travelEndDate: "", // "string"
      person: 1, // 0
      userState: 0, // 0
      days: 1,
    };

    $scope.ispassxy = true;

    // 获取向导详情
    $$GuideService.getGuideDetail($scope.guideId)
      .then(function(res) {
        $scope.guideVO = res.data.guideInfoVO;
        // $scope.guideVO.servicePrice = pricetodoubule($scope.guideVO.servicePrice);
      });

    // 价格计算
    $scope.calculatePrice = function() {
      $scope.orderGuideData.days = parseInt($scope.orderGuideData.days);
      $scope.orderGuideData.person = parseInt($scope.orderGuideData.person);
      if ($scope.orderGuideData.days <= 0) {
        $scope.orderGuideData.days = 1;
      }
      if ($scope.orderGuideData.person <= 0) {
        $scope.orderGuideData.person = 1;
      }
      $scope.orderGuideData.person = parseInt($scope.orderGuideData.person);
      // console.log($scope.guideVO.servicePrice);
      // console.log($scope.orderGuideData.days);
      // console.log($scope.orderGuideData.person);
      $scope.orderGuideData.price = parseFloat(($scope.guideVO.servicePrice * $scope.orderGuideData.days * $scope.orderGuideData.person).toFixed(2));
      // console.info($scope.orderGuideData.price);
      // console.info($scope.guideVO.servicePrice * $scope.orderGuideData.person * $scope.orderGuideData.person);
      return $scope.orderGuideData.price;
    };

    // 提交预定向导表单
    $scope.orderGuideform = function() {
      if (!$scope.ispassxy) {
        $ionicLoading.show({
          template: "请同意平台法律条款",
          noBackdrop: true,
          duration: 1500
        });
        return;
      }



      $scope.orderGuideData.travelEndDate = new Date($scope.orderGuideData.travelStartDate.getTime() + $scope.orderGuideData.days * 24 * 60 * 60 * 1000).format('yyyy-MM-dd h:m');
      // $scope.calculatePrice();
      // console.log($scope.orderGuideData.travelEndDate);
      // 获取订单Id getOrderId
      $$UserOrderService.getOrderId()
        .then(function(res_getOrderId) {
          console.log("guideorder/id id:");
          // 预定表单提交
          $$UserOrderService.orderGuide($scope.guideId, res_getOrderId.data.id, $scope.orderGuideData)
            .then(function(res) {
              $ionicLoading.show({
                template: "提交成功",
                noBackdrop: true,
                duration: 1500
              });
              $timeout(function() {
                $location.path("ordervisitor");
              }, 2000);
            });

        });


    };

  });
